import { Table, Tag, Space, Button, Switch } from 'antd';
import { TimerVO } from '@/types/timer';
import { formatDateTime } from '@/utils/dateUtils';

interface TimerListProps {
  timers: TimerVO[];
  onEdit: (timer: TimerVO) => void;
  onDelete: (id: number) => void;
  onStatusChange: (id: number) => void;
}

const TimerList: React.FC<TimerListProps> = ({ timers, onEdit, onDelete, onStatusChange }) => {
  const columns = [
    {
      title: '任务名称',
      dataIndex: 'name',
      key: 'name',
    },
    {
      title: '设备',
      dataIndex: 'deviceName',
      key: 'deviceName',
      render: (text: string, record: TimerVO) => (
        <span>{text} ({record.deviceLocation})</span>
      ),
    },
    {
      title: '执行间隔',
      dataIndex: 'intervalMinutes',
      key: 'intervalMinutes',
      render: (minutes: number) => `${minutes} 分钟`,
    },
    {
      title: '目标状态',
      dataIndex: 'targetStatus',
      key: 'targetStatus',
      render: (status: boolean) => (
        <Tag color={status ? 'green' : 'red'}>
          {status ? '开启' : '关闭'}
        </Tag>
      ),
    },
    {
      title: '状态',
      dataIndex: 'status',
      key: 'status',
      render: (status: boolean, record: TimerVO) => (
        <Switch
          checked={status}
          onChange={() => onStatusChange(record.id)}
          checkedChildren="启用"
          unCheckedChildren="禁用"
        />
      ),
    },
    {
      title: '下次执行时间',
      dataIndex: 'nextExecuteTime',
      key: 'nextExecuteTime',
      render: (time: string) => formatDateTime(time),
    },
    {
      title: '操作',
      key: 'action',
      render: (_: any, record: TimerVO) => (
        <Space size="middle">
          <Button type="link" onClick={() => onEdit(record)}>编辑</Button>
          <Button type="link" danger onClick={() => onDelete(record.id)}>删除</Button>
        </Space>
      ),
    },
  ];

  return (
    <Table
      columns={columns}
      dataSource={timers}
      rowKey="id"
      pagination={{ pageSize: 10 }}
    />
  );
};

export default TimerList; 